package org.quickjsf.core.base.service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.quickjsf.core.base.dao.IBaseDao;
import org.quickjsf.core.base.dao.PageModel;
import org.quickjsf.core.base.viewmodel.vo.FieldFilter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component("absAaseService")
public abstract class AbsBaseService<T> implements IBaseService<T>{
	
	protected IBaseDao<T> baseDao;
	
	public AbsBaseService(IBaseDao<T> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Transactional
	public void remove(T entity) {
		baseDao.delete(entity);
	}
	
	@Transactional
	public void removeAll(Collection<T> entities) {
		baseDao.deleteAll(entities);
	}
	
	@Transactional(readOnly=true)
	public T getById(Serializable id) {
		return baseDao.queryById(id);
	}
	
	public T fetchAllById(final Serializable id) {
		return baseDao.fetchAllById(id);
	}
	
	@Transactional(readOnly=true)
	public List<T> getAll() {
		return baseDao.queryAll();
	}
	
	@Transactional
	public Serializable save(T entity) {
		return baseDao.save(entity);
	}
	
	@Transactional
	public void update(T entity) {
		baseDao.update(entity);
	}
	
	@Transactional(readOnly=true)
	public PageModel<T> getTableList(final int first, final int pageSize,
			final String sortField, final String sortName, final List<Criterion> criterions) {
		return baseDao.queryTable(first, pageSize, sortField, sortName, criterions);
	}
	
	@Transactional(readOnly=true)
	public PageModel<T> getTableFilterList(int first, int pageSize, 
			String sortField, String sortName, List<FieldFilter> fieldFilters) {
				return baseDao.getTableFilterList(first, pageSize, sortField, sortName, fieldFilters);
		
	}
}
